<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/4 0004
 * Time: 0:05
 */

namespace App\Http\Controllers\Blog;

use App\Http\Controllers\Controller;
use App\Http\Controllers\PublicController;

use Illuminate\Http\Request;
use App\Common\ErrorDefine;
use Crypt;
use App\Model\Fg_blog;
use App\Model\Fg_content;
use App\Model\Fg_cate;
use App\Model\Fg_keyword;
class BlogController extends Controller
{
    private $Blog;
    private $Content;
    private $Cate;
    private $Keyword;
    private $Public;
    private $page = 0;
    private $limit = 10;

    public function __construct(PublicController $public)
    {
        $this->Blog = new Fg_blog();
        $this->Content = new Fg_content();
        $this->Cate = new Fg_cate();
        $this->Keyword = new Fg_keyword();
        $this->Public = $public;

    }


    /**
     * @param Request $request
     * 添加博客
     */
    public function addBlog(Request $request)
    {
        $params =[];

        //判断传值是否为空
        if(empty($request->all()))
        {
            self::ShowError(ErrorDefine::INVALID_PARAMS);
            return;
        }

        //判断是否登录
        if(!$request->has('token') || empty($request->input('token')))
        {
            self::showError(ErrorDefine::ACCOUNT_LOGIN_EXPIRED);
            return;
        }

        //判断标题是否存在
        if(empty($request->input('title')) || !$request->has('title'))
        {
            self::ShowError(ErrorDefine::INVALID_PARAMS);
            return;
        }

        //判断文章正文是否存在
        if(empty($request->input('content')) || !$request->has('content'))
        {
            self::ShowError(ErrorDefine::INVALID_PARAMS);
            return;
        }

        //判断是否分类
        if(empty($request->input('cate_id')) || !$request->has('cate_id'))
        {
            self::ShowError(ErrorDefine::INVALID_PARAMS);
            return;
        }

        //判断是否有简述

        if(empty($request->input('des')) || !$request->has('des'))
        {
            self::ShowError(ErrorDefine::INVALID_PARAMS);
            return;
        }

        //判断是否置顶
        if(empty($request->input('is_top')) || !$request->has('is_top'))
        {
            $params['is_top'] = 0;
        }

        //作者默认为自己
        if(empty($request->input('author')) || !$request->has('author'))
        {
            $params['author'] = '董富贵';
        }

        //判断是否含有图片,有则序列化
        if($request->has('img') && !empty($request->input('img')))
        {
            $params['img'] = serialize($request->input('img'));
        }

        //判断是否含有标签
        if($request->has('key_id') && !empty($request->input('key_id')))
        {
            $params['key_id'] = $request->input('key_id');
        }

        if($request->input('cate_id') != 1){
            //判断是否含有关键词
            if($request->has('note') && !empty($request->input('note')))
            {
                $params['note'] = $request->input('note');
            }
        }

        $content['content'] = $request->input('content');
        $content_id = $this->Content->insertGetId($content);

        if(!$content_id){
            self::ShowError(ErrorDefine::DATABASE_ERROR);
            return;
        }

        $params['content_id'] = $content_id;
        $params['title'] = $request->input('title');
        $params['cate_id'] = $request->input('cate_id');

        $params['des'] = $request->input('des');
        $params['is_top'] = $request->input('is_top');
        $params['updated_at'] = date('Y-m-d H:i:s');

        try{

            $this->Blog->insert($params);

            return self::ajaxReturn(0,'success');

        }catch (\Exception $exception){

            self::ShowError(ErrorDefine::DATABASE_ERROR);
            return;
        }


    }


    /**
     * @param Request $request
     * 博客列表
     */
    public function getBlogList(Request $request)
    {
        //判断传值是否为空
        if(empty($request->all()))
        {
            self::ShowError(ErrorDefine::INVALID_PARAMS);
            return;
        }

//        //判断是否登录
        if(!$request->has('token') || empty($request->input('token')))
        {
            self::showError(ErrorDefine::ACCOUNT_LOGIN_EXPIRED);
            return;
        }

        if($request->input('page'))
        {
            $this->page  = $request->input('page');
            if($this->page){
                $this->page = $this->page - 1;
                if(is_numeric($this->page)){
                    if($this->page<0){
                        $this->page = 0;
                    }
                }
            }
        }

        if($request->input('limit'))
        {
            $this->limit = $request->input('limit');
            if($this->limit){
                if($this->limit <=0 ){
                    $this->limit =  50;
                }
            }
        }

        $data = $this->Blog->leftJoin('fg_cate','fg_blog.cate_id','=','fg_cate.id')
            ->offset($this->limit*$this->page)->limit($this->limit)
            ->orderBy('fg_blog.created_at',"desc")
            ->get(['fg_blog.id','fg_blog.title','fg_blog.key_id','fg_blog.note','fg_blog.author','fg_blog.is_top','fg_blog.created_at','fg_cate.cate_name'])
            ->toArray();

        $count = $this->Blog->count();


        if(!empty($data)){
            foreach ($data as &$value){
                if(empty($value['key_id'])){
                    continue;
                }
                $value['keyword'] = $this->getKeywordById($value['key_id']);
            }
        }

        return self::ajaxReturn(0,'success',$data,$count);



    }


    public function previewBlog(Request $request){
        //判断传值是否为空
        if(empty($request->all()))
        {
            self::ShowError(ErrorDefine::INVALID_PARAMS);
            return;
        }

        //判断是否登录
        if(!$request->has('token') || empty($request->input('token')))
        {
            self::showError(ErrorDefine::ACCOUNT_LOGIN_EXPIRED);
            return;
        }

        if(!$request->has('action') || empty($request->input('action')))
        {
            if(!$request->has('blog_id') || empty($request->input('blog_id')))
            {
                self::showError(ErrorDefine::INVALID_PARAMS);
                return;
            }
        }else{
            $data = $this->Blog->leftJoin('fg_content','fg_blog.content_id','=','fg_content.id')->orderBy('fg_blog.updated_at','desc')->first()->toArray();

            return self::ajaxReturn(0,'success',$data);
        }

    }

    /**
     * @param $key_id
     * @return string
     * 根据id获取关键词
     */
    public function getKeywordById($key_id)
    {

        $key['key_id'] = $key_id;
        $keyarr = $this->Keyword->whereIn('id',$key)->get(['keyword'])->toArray();

        $keyword = implode(',',$keyarr[0]);
        return $keyword;
    }


}